package com.example.administrator.szb.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.v4.util.SparseArrayCompat;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableStringBuilder;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.widget.Checkable;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * 封装Recycleview通用adapter
 * 注意这个adpterb不支持gridview设置head
 * Created by pdf on 2017/6/22.
 */

public abstract class BaseAdapter<T> extends RecyclerView.Adapter{
    //添加header和footer的标识
    private static final int BASE_ITEM_TYPE_HEADER = 10000;
    private static final int BASE_ITEM_TYPE_FOOTER = 20000;

    private int setLongOfList;//定制recyclerview的长度
    private Boolean setListLong = false;

    private SparseArrayCompat<View> mHeaderViews = new SparseArrayCompat<>();
    private SparseArrayCompat<View> mFootViews = new SparseArrayCompat<>();

    private Context mContext;
    public List<T> mDatas =  new ArrayList<>();;
    private int mLayoutId;
    private OnItemClickListener mItemClickListener;
    private OnLongItemClickListener mLongItemClickListener;
    private IsClickable mIsClickable;
    boolean info_err = false;


    public BaseAdapter(Context context, List<T> mDatas) {
        this.mContext = context;
        this.mDatas = mDatas;
        if (this.mDatas==null){
            this.mDatas = new ArrayList<>();
        }
    }

    public BaseAdapter(Context context, List<T> mDatas,boolean info_err) {
        this.mContext = context;
        this.mDatas = mDatas;
        if (this.mDatas==null){
            this.mDatas = new ArrayList<>();
        }
        this.info_err = info_err;
    }

    public BaseAdapter(Context context, List<T> mDatas,int setLongOfList) {
        this.mContext = context;
        this.mDatas = mDatas;
        if (this.mDatas==null){
            this.mDatas = new ArrayList<>();
        }
        this.setLongOfList = setLongOfList;
        setListLong = true;
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (mHeaderViews.get(viewType) != null)
        {
            BaseViewHolder holder = new BaseViewHolder(mHeaderViews.get(viewType));
            return holder;

        } else if (mFootViews.get(viewType) != null)
        {
            BaseViewHolder holder = new BaseViewHolder(mFootViews.get(viewType));
            return holder;
        }


        View view = LayoutInflater.from(mContext).inflate(getLayoutId(viewType), parent, false);
        BaseViewHolder holder = new BaseViewHolder(view);
        return holder;
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if(isHeaderViewPos(position)){
            return;
        }
        if (isFooterViewPos(position)){
            return;
        }
        try{
            convert(position,(BaseViewHolder) holder,mDatas.get(position-mHeaderViews.size()));
        }
        catch (Exception e){
            e.toString();
        }


        if(mIsClickable != null){
            ((BaseViewHolder) holder).mItemView.setEnabled(mIsClickable.isClickable());
        }


        if (mItemClickListener != null) {
            ((BaseViewHolder) holder).mItemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mItemClickListener.onItemClick(v, position);
                }
            });
        }
        if (mLongItemClickListener != null) {
            ((BaseViewHolder) holder).mItemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    mLongItemClickListener.onLongItemClick(v, position);
                    return true;
                }
            });
        }


    }

    @Override
    public int getItemCount() {
        if (setListLong && setLongOfList<=mDatas.size()){
            return setLongOfList+getHeadersCount() + getFootersCount();
        }

        return mDatas.size()+getHeadersCount() + getFootersCount();
    }
    @Override
    public int getItemViewType(int position) {
        if (isHeaderViewPos(position))
        {
            return mHeaderViews.keyAt(position);//通过位置获取mHeaderViews中的key
        } else if (isFooterViewPos(position))
        {
            if (setListLong){
                return mFootViews.keyAt(position - getHeadersCount() - setLongOfList);
            }
            return mFootViews.keyAt(position - getHeadersCount() - mDatas.size());
        }
        return position;
    }


    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public interface IsClickable{
        boolean isClickable();
    }

    public interface OnLongItemClickListener {
        void onLongItemClick(View view, int postion);
    }

    //设置点击事件方法
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mItemClickListener = listener;
    }
    //设置长点击事件方法
    public void setonLongItemClickListener(OnLongItemClickListener listener) {
        this.mLongItemClickListener = listener;
    }

    //设置长点击事件方法
    public void isClickable(IsClickable listener) {
        this.mIsClickable = listener;
    }



    public abstract void convert(int position, BaseViewHolder holder, T t);

    //根据view的位置设置不同的布局
    public abstract int getLayoutId(int viewType);

    private boolean isHeaderViewPos(int position)
    {
        return position < getHeadersCount();
    }

    private boolean isFooterViewPos(int position) {
        if(setListLong){
            return position >= getHeadersCount() + setLongOfList;
        }
        return position >= getHeadersCount() + mDatas.size();
    }


    public void addHeaderView(View view) {
        mHeaderViews.put(mHeaderViews.size() + BASE_ITEM_TYPE_HEADER, view);
    }

    public void addFootView(View view) {
        mFootViews.put(mFootViews.size() + BASE_ITEM_TYPE_FOOTER, view);
    }

    public int getHeadersCount()
    {
        return mHeaderViews.size();
    }

    public int getFootersCount()
    {
        return mFootViews.size();
    }


    /**
     * 通用的ViewHodler
     */
    public class BaseViewHolder extends RecyclerView.ViewHolder{
        SparseArray<View> mViews;
        View mItemView;

        public BaseViewHolder(View itemView) {
            super(itemView);
            mItemView = itemView;
            mViews = new SparseArray<View>();
        }

        public <T extends View> T getView(int viewId){
            View view = mViews.get(viewId);
            if(view == null){
                view = mItemView.findViewById(viewId);
                mViews.put(viewId,view);
            }
            return (T)view;
        }
        public BaseViewHolder setText(int viewId, int resId) {
            TextView textView = getView(viewId);
            textView.setText(resId);
            return this;
        }
        public BaseViewHolder setText(int viewId, String text) {
            TextView textView = getView(viewId);
            textView.setText(text);
            return this;
        }
        public BaseViewHolder setText(int viewId, SpannableStringBuilder text) {
            TextView textView = getView(viewId);
            textView.setText(text);
            return this;
        }
        public BaseViewHolder setImageResource(int viewId, int resId) {
            ImageView view = getView(viewId);
            view.setImageResource(resId);
            return this;
        }
        public BaseViewHolder setImageBitmap(int viewId, Bitmap bitmap) {
            ImageView view = getView(viewId);
            view.setImageBitmap(bitmap);
            return this;
        }
        public BaseViewHolder setImageDrawable(int viewId, Drawable drawable) {
            ImageView view = getView(viewId);
            view.setImageDrawable(drawable);
            return this;
        }
        public BaseViewHolder setBackgroundColor(int viewId, int color) {
            View view = getView(viewId);
            view.setBackgroundColor(color);
            return this;
        }
        public BaseViewHolder setBackgroundResource(int viewId, int backgroundRes) {
            View view = getView(viewId);
            view.setBackgroundResource(backgroundRes);
            return this;
        }
        public BaseViewHolder setTextColor(int viewId, int textColor) {
            TextView view = getView(viewId);
            view.setTextColor(textColor);
            return this;
        }
        @SuppressLint("NewApi")
        public BaseViewHolder setAlpha(int viewId, float value) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                getView(viewId).setAlpha(value);
            } else {
                // Pre-honeycomb hack to set Alpha value
                AlphaAnimation alpha = new AlphaAnimation(value, value);
                alpha.setDuration(0);
                alpha.setFillAfter(true);
                getView(viewId).startAnimation(alpha);
            }
            return this;
        }
        public BaseViewHolder setVisible(int viewId, boolean visible) {
            View view = getView(viewId);
            view.setVisibility(visible ? View.VISIBLE : View.GONE);
            return this;
        }
        public BaseViewHolder setTag(int viewId, Object tag) {
            View view = getView(viewId);
            view.setTag(tag);
            return this;
        }
        public BaseViewHolder setTag(int viewId, int key, Object tag) {
            View view = getView(viewId);
            view.setTag(key, tag);
            return this;
        }
        public BaseViewHolder setChecked(int viewId, boolean checked) {
            Checkable view = (Checkable) getView(viewId);
            view.setChecked(checked);
            return this;
        }
        /**
         * 关于事件监听
         */
        public BaseViewHolder setOnClickListener(int viewId, View.OnClickListener listener) {

            View view = getView(viewId);
            view.setOnClickListener(listener);
            return this;
        }
        public BaseViewHolder setOnTouchListener(int viewId, View.OnTouchListener listener) {
            View view = getView(viewId);
            view.setOnTouchListener(listener);
            return this;
        }
        public BaseViewHolder setOnLongClickListener(int viewId, View.OnLongClickListener listener) {
            View view = getView(viewId);
            view.setOnLongClickListener(listener);
            return this;
        }
    }
}
